<?php
	require("frmCookie.php");
	
	switch($_GET[iOpcion])
	{
		case 2:
		{
			$mySQL = "DELETE FROM tiposusosxstock WHERE idStock=" . $_POST[txtIdStock];
			$mySQL .=";";
			//executeSQL($mySQL);
			
			if ($_POST[txtUsos] != "")
			{
				$i = 0;
				$iPos = 1;
				while($iPos != 0)
				{
					$iPos = strpos($_POST[txtUsos], "-", $i);
					if ($iPos != 0)
					{
						$mySQL .= "INSERT INTO tiposusosxstock values(" . substr($_POST[txtUsos], $i, $iPos - $i) . ", " . $_POST[txtIdParte] . ", '0', '" . $_POST[txtFactura] . "', " . $_POST[txtIdStock] . ")";
						$mySQL .=";";
						//echo $mySQL;
						//executeSQL($mySQL);
						$i = $iPos + 1;	
					}
					else
					{					
						$mySQL .= "INSERT INTO tiposusosxstock values(" . substr($_POST[txtUsos], $i) . ", " . $_POST[txtIdParte] . ", '0', '" . $_POST[txtFactura] . "', " . $_POST[txtIdStock] . ")";
						$mySQL .=";";
						//echo $mySQL;
						//executeSQL($mySQL);
					}
				}
			}
			
			$mySQL .= "UPDATE stock SET cantidad=" . $_POST['txtCantidad'] . ", codigoreferencia = '".$_POST['txtCodigoDeReferencia']."', simlock = '".$_POST['txtSimlock']."' WHERE idStock=" . $_POST[txtIdStock];
			$mySQL .=";";
			//echo $mySQL;
			//exit;
			transactionSQL($mySQL,true);
			
			?>
			<script language="JavaScript" type="text/javascript">
			<!--
				window.location.href = "main.php";
			//-->
			</script>
		  	<?php
			break;
		}
	}	
?>
<html>
	<head>
    	<?php
		require("frmHeader.php");
		?>
		<script language="javascript" src="scripts/ajax.js"></script>
		<script language="javascript" src="scripts/validar.js"></script>
		<script language="javascript">
		
			function addUso()
			{
				if (frm.cboUsos.length > 0)
				{
					frm.grdUsos.options[frm.grdUsos.length] = new Option(frm.cboUsos.options[frm.cboUsos.selectedIndex].text, frm.cboUsos.options[frm.cboUsos.selectedIndex].value);
					frm.cboUsos.options[frm.cboUsos.selectedIndex] = null;
				} 
			}
			
			function removeUso()
			{
				if (frm.grdUsos.length > 0 && frm.grdUsos.selectedIndex != -1)
				{
					frm.cboUsos.options[frm.cboUsos.length] = new Option(frm.grdUsos.options[frm.grdUsos.selectedIndex].text, frm.grdUsos.options[frm.grdUsos.selectedIndex].value);
					frm.grdUsos.options[frm.grdUsos.selectedIndex] = null;
				} 
			}
			
			function valida()
			{
				if (frm.grdUsos.length == 0)
					{
						alert("Debe asociar al menos un Tipo de Uso.");
						return false;
					}
				
				for(i = 0; i < frm.grdUsos.length; i++)
				{
					if (i == 0)
						frm.txtUsos.value = frm.grdUsos.options[i].value;
					else
						frm.txtUsos.value = frm.txtUsos.value + "-" + frm.grdUsos.options[i].value;
				}
				
				if(!IsNumeric(frm.txtCantidad.value))
				{
					alert("La cantidad debe ser un valor numerico.");
					frm.txtCantidad.focus();
					return false;
				}
				
				document.frm.submit();
			}
			
		</script>
	</head>
	<body>
    	<table cellpadding="0" cellspacing="0" align="center" class="tablaPrincipal">
        	<tr>
            	<td align="center">
                	<?php
					require("frmSession.php"); 
					
					//Redirecciona a SINPERMISOS.PHP en caso de no tener permisos
					
					if (!(in_array(39000, $_SESSION[permisos])))
					{
					?>
					  <script language="JavaScript" type="text/javascript">
					  <!--
					  window.location.href = "sinPermisos.php"
					  //-->
					  </script>
					<?php
					}
					$titulo = "Consulta de STOCK";
					
					?>
                </td>
            </tr>
            <tr>
            	<td align="center">
                	<?php
					require("frmTitulo.php");
					?>
                </td>
            </tr>
            <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
            	<td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="menuPrincipal">
                    	<tr>
                        	<td>
								<?php
                                require("frmMenu.php");
                                ?>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
                <td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="tituloPantalla">
                    	<tr>
                        	<td align="center">
                            	<p class="texto" align="left"><?php echo $titulo; ?></p>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
             <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
                <td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="estadoPantalla">
                    	<tr>
                        	<td align="center">
                            	<p class="texto" align="left"><?php echo $mensaje; ?></p>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
             <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
                <td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="contenedorPrincipal">
                    	<tr>
                        	<td align="">
                        		<form name="frm" action="cambioUsoStock.php?iOpcion=2" method="post" onSubmit="return valida();">
                        			<?php
                        			$i = 0;
                        			
                    				$mySQL = "SELECT idTipoUso FROM tiposusosxstock WHERE idStock = " . $_GET[iStock];
                    				$result = readSQL($mySQL);
	  				                foreach($result as $value)
	  				                {    
  					                	if($i == 0)
  					                		$filtro = " WHERE idTipoUso NOT IN(" . $value[0];
  					                	else	
  					                		$filtro = $filtro . ", " . $value[0];
				                		$i++;
	  				                }
	  				                if($filtro != "")
	  				                	$filtro = $filtro . ")";
	  				                
	  				                //$mySQL = "SELECT S.*, P.descripcionInterna, P.idTipoParte, M.nombre AS MONEDA FROM stock S, partes P, monedas M WHERE M.idMoneda = S.idMoneda AND P.idParte = S.idParte AND idStock = " . $_GET[iStock];
									$mySQL = "Select S.*, P.descripcionInterna, P.idTipoParte, M.nombre AS MONEDA, S.simlock FROM stock S left join partes P on S.idParte = P.idParte left join monedas M on S.idMoneda = M.idMoneda WHERE S.idStock = " . $_GET[iStock];
									//echo $mySQL;
	  				                $result = readSQL($mySQL);
	  				                foreach($result as $value)
	  				                {
										$simlock = $value['simlock'];
										$codigoDeReferencia = $value['codigoReferencia'];
	  				                	$parte = $value['descripcionInterna'];
	  				                	$cantidad = $value['cantidad'];
	  				                	$factura = $value['nroFactura'];
	  				                	$precio = $value['precio'];
	  				                	$moneda = $value['MONEDA'];
	  				                	$idParte = $value['idParte'];
										$idTipoParte = $value['idTipoParte'];
	  				                }
                        			?>
		                			<table>
		                				<tr>
		                					<td>Parte: </td>
		                					<td><b><?php echo $parte; ?></b></td>
		                				</tr>
                                        	<?php
											if($idTipoParte == 3 || $idTipoParte == 4)
											{
											?>
                                                <tr>
                                                    <td>Simlock: </td>
                                                    <td><input type="text" name="txtSimlock" value="<?php echo $simlock; ?>" size="15" maxlength="15"></td>
                                                </tr>
                                                <tr>
                                                    <td>Codigo de referencia: </td>
                                                    <td><input type="text" name="txtCodigoDeReferencia" value="<?php echo $codigoDeReferencia; ?>" size="15" maxlength="15"></td>
                                                </tr>
                                            <?php
											}
											?>
		                				<tr>
		                					<td>Cantidad: </td>
		                					<td><input type="text" name="txtCantidad" value="<?php echo $cantidad; ?>" size="2" maxlength="4"></td>
		                				</tr>
		                				<tr>
		                					<td>Factura: </td>
		                					<td><b><?php echo $factura; ?></b></td>
		                				</tr>
		                				<tr>
		                					<td>Precio: </td>
		                					<td><b><?php echo $precio; ?></b></td>
		                				</tr>
		                				<tr>
		                					<td>Moneda: </td>
		                					<td><b><?php echo $moneda; ?></b></td>
		                				</tr>
		                				<tr>
		                                    <td>*Tipos de Uso: </td>
		                                    <td>
												<select name="cboUsos">
											   	<?php
												$mySQL = "SELECT * FROM tiposusos " . $filtro . " ORDER BY nombre";  
												echo $mySQL;
												$result = readSQL($mySQL);
												foreach($result as $value)
												{    
													echo "<option value='" . $value[0] . "'>" . $value[1] . "</option>";	
												}
												?>
												</select>
		                                    </td>
		                                    <td>
		                						<a href="#" onClick="addUso();" class="mas"><img src="images/mas.png" title="Agregar" class="imagen"></a>
											</td>
		                                </tr>
		                				<tr>
		                					<td colspan="2">Tipos de uso asociados:</td>
		                				</tr>
		                				<tr>
		                					<td colspan="2">
		                						<select name="grdUsos" size="4" style="width:100%;">
		                						<?php
			                    				$mySQL = "SELECT TU.idTipoUso, TU.nombre FROM tiposusosxstock TUS, tiposusos TU WHERE TU.idTipoUso = TUS.idTipoUso AND TUS.idStock = " . $_GET[iStock];
			                    				$result = readSQL($mySQL);
				  				                foreach($result as $value)
				  				                {    
			  					                	echo "<option value='" . $value['idTipoUso'] . "'>" . $value['nombre'] . "</option>";
				  				                }
			                        			?>
		                						</select>
		                					</td>
		                					<td style="vertical-align:top">
		                						<a href="#" onClick="removeUso();" class="menos"><img src="images/menos.png" title="Quitar" class="imagen"></a>
		                					</td>
		                				</tr>
		                				<tr>
		                					<td colspan="2" align="center"><input type="button" name="cmdEnviar" value="Modificar" onClick="valida();" /></td>
		                				</tr>
		                			</table>
		                			<input type="hidden" name="txtUsos" />
		                			<input type="hidden" name="txtIdParte" value="<?php echo $idParte;?>" />
		                			<input type="hidden" name="txtFactura" value="<?php echo $factura;?>" />
		                			<input type="hidden" name="txtIdStock" value="<?php echo $_GET[iStock];?>" />
        		            	</form>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
	</body>
</html>